<?php
namespace app\api\controller;
header('Content-Type: text/html;charset=utf-8');
header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型



class Activity extends Basic
{

    public function activitylist()
    {
        $limit = input("limit",6);
        $list=Db('activity')->limit($limit)->where(['status'=>1])->order("sort desc,update_time desc")->select();
        if($list) {
            foreach ($list as $key => $value) {
                $list[$key]['image'] = $this->exchange_img($list[$key]['image']);
            }
        }
        json_success($list);
    }

    public function activityinfo()
    {
        $limit = input("limit",70);
        $id = input("id",0);
        $data = [];
        $data['info'] = Db('activity')->where(['id'=>$id])->find();
        isset($data['info']['top_image']) && $data['info']['top_image'] = $this->exchange_img($data['info']['top_image']);

        if($infos = Db('activity_info')->field('id,name,sub_title,image,first_image,sub_name,descp,address,tid')->where(['aid'=>$id])->select()) {
            $allTags = Db('tag')->field('id,name')->where(['status'=>1])->order('sort desc')->select();
            if($allTags) {
                foreach ($allTags as $tag) {
                    $thisdata = [];
                    foreach ($infos as $info) {
                        if($info['first_image']) {
                            $info['image'] = $this->exchange_img($info['first_image']);
                        } else {
                            $info['image'] = $this->exchange_img($info['image']);
                        }
                        if($info['tid']==$tag['id']) {
                             $thisdata[] = $info;
                        }
                    }
                    $thisdata && $data['list'][] = ['name'=>$tag['name'],'id'=>$tag['id'],'data'=>array_slice($thisdata, 0,$limit)];
                }
            }



            // $ids = $tags = [];
            // foreach ($infos as $key => $value) {
            //     $value['tid'] && $ids[] = $value['tid'];
            // }
            // // var_dump($ids);exit;
            // if($ids) {
            //     // 所有id
            //     $map['id'] = ['in',implode(',', $ids)];
            //     $allTags = Db('tag')->field('id,name')->where($map)->where(['status'=>1])->order('sort desc')->select();
            //     // var_dump($allTags);exit;
            //     if($allTags) {
            //         foreach ($allTags as $t) {
            //             $tags[$t['id']] = $t['name'];
            //         }
            //     }
            //     if($tags) {
            //         foreach ($tags as $t=> $tagobj) {
            //             foreach ($infos as $key => $value) {
            //                 if($value['tid']==$t) {
            //                      $value['tid'] && $data['list'][] = ['name'=>$tagobj,'id'=>$value['tid'],'data'=>Db('activity_info')->where(['tid'=>$value['tid']])->where(['aid'=>$id])->limit($limit)->select()];
            //                 }
                           
            //             }
            //         }
            //     }
            //     // var_dump($tags);exit;
                    
            // }
        }
        json_success($data);
    }

    public function articlelist()
    {
        $page = input("page",1);
        $aid = input("aid",0);
        $tid = input("tid",0);
        $limit = input('limit',10);
        $dataList['list']=Db('activity_info')->field('id,name,sub_title,image,first_image,sub_name,descp,address,tid')->where('tid',$tid)->where(['status'=>1])->where('aid',$aid)->page($page,$limit)->order('sort desc,update_time desc')->select();
        $dataList['count']=Db('activity_info')->where('tid',$tid)->where('aid',$aid)->count();
        if($dataList['list']) {
            foreach ($dataList['list'] as $key => $value) {
                if($dataList['list'][$key]['first_image']) {
                    $dataList['list'][$key]['image'] = $this->exchange_img($dataList['list'][$key]['first_image']);
                } else {
                    $dataList['list'][$key]['image'] = $this->exchange_img($dataList['list'][$key]['image']);
                }
            }
        }
        json_success($dataList);
    }
    public function articleinfo()
    {
        $id = input("id",6);
        $info = Db('activity_info')->where(['id'=>$id])->find();
        $info['content'] = str_replace('<img ', '<img style="max-width:100%;height:auto" ', $info['content']);
        $img_info = getimagesize($info['image']);
        if($img_info[0]>$img_info[1]) {
            $info['is_heng'] = 1;
        } else {
            $info['is_heng'] = 0;
        }
        json_success($info);
    }


}